new Vue({
    el: "#app",
    data: {
        fullscreenLoading: false,
        screenLoading: false,
        dialogDeleteVisible: false,
        dialogNewVisible: false,
        dialogUpdateVisible: false,
        dialogChangeVisible:false,
        status:0,
        versionData: [],
        newVersion:{
            name:'com.loanmarket',
            number:'',
            filePath:''
        },
        fileList:[],
        filePath:'',
        updateVersion: {},
        id:null,
        pageNo:0,
        total:0,
        pageSize:20,
        pages:0
    },
    mounted: function () {
        var _this = this;
        _this.loadVersionList();
    },
    methods: {
        trim: function (str) {
            if (isNaN(str)) {
                return str.replace(/(^\s*)|(\s*$)/g, "");
            } else {
                return str
            }
        },
        loadVersionList: function () {
            var _this = this;
            axios.get(request_prefix + "/cash/version/list")
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.versionData = res.data.data.dataList;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        handleCurrentChange: function () {
            var _this = this;
            axios.get(request_prefix + "/cash/version/list",{
                params:{
                    pageNo:_this.pageNo + 1
                }
            })
                .then(function (res) {
                    if (res.data.code === 0) {
                        _this.pageNo = parseInt(res.data.data.pageNo);
                        _this.total = parseInt(res.data.data.total);
                        _this.pageSize = parseInt(res.data.data.pageSize);
                        _this.pages = parseInt(res.data.data.pages);
                        _this.versionData = res.data.data.dataList;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        },
        filterStatus:function (field) {
            var result = "";
            switch(field) {
                case 0 : result = "新建";break;
                case 1 : result = "已发布";break;
                case 2 : result = "已作废";break;
                default:
                    result = "";
                    break;
            }
            return result;
        },
        toNewDialog: function () {
            var _this = this;
            _this.fileList = [];
            _this.newVersion.filePath = '';
            _this.filePath = '';
            _this.dialogNewVisible = true;
        },
        confirmCreate: function () {
            var _this = this;
            if(_this.filePath == null || _this.filePath == "") {
                _this.$message.warning("请完成apk上传");
                return;
            }
            _this.newVersion.filePath = _this.filePath;
            if(_this.newVersion.number == null || _this.newVersion.number == '') {
                _this.$message.warning("版本号不能为空");
                return;
            }
            axios.post(request_prefix + "/cash/version/add",_this.newVersion)
                .then(function (res) {
                    if(res.data.code == 0) {
                        _this.loadVersionList();
                        _this.newVersion = {};
                        _this.dialogNewVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                })
        },
        toEditDialog: function (id) {
            var _this = this;
            axios.get(request_prefix + "/cash/version/detail",{
                params:{
                    id:id
                }
            })
                .then(function (res) {
                    if(res.data.code === 0) {
                        var result = {};
                        _this.fileList = [];
                        var resultData = res.data.data;
                        result.name = resultData.name;
                        result.url = resultData.filePath;
                        _this.fileList.push(result);
                        _this.updateVersion.id = resultData.id;
                        _this.updateVersion.name = resultData.name;
                        _this.updateVersion.filePath = resultData.filePath;
                        _this.updateVersion.number = resultData.number;
                        _this.filePath = resultData.filePath;
                        _this.newVersion.filePath = '';
                        _this.dialogUpdateVisible = true;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                });
        },
        confirmUpdate: function () {
            var _this = this;
            if(_this.filePath == null || _this.filePath == "") {
                _this.$message.warning("请完成apk上传");
                return;
            }
            _this.updateVersion.filePath = _this.filePath;
            if(_this.updateVersion.number == null || _this.updateVersion.number == '') {
                _this.$message.warning("版本号不能为空");
                return;
            }
            axios.post(request_prefix + "/cash/version/update",_this.updateVersion)
                .then(function (res) {
                    if(res.data.code === 0) {
                        _this.loadVersionList();
                        _this.fileList = [];
                        _this.filePath = '';
                        _this.updateVersion = {};
                        _this.dialogUpdateVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
                })
        },
        deleteVersion: function (id) {
            var _this = this;
            _this.id = id;
            _this.dialogDeleteVisible = true;
        },
        confirmDelete: function () {
            var _this = this;
            var params = new URLSearchParams();
            var id = _this.id;
            if(id == null) {
                _this.$message.warning("id不能为空");
                return;
            }
            params.append("id",id);
            axios.post(request_prefix + "/cash/version/delete",params)
                .then(function (res) {
                    if(res.data.code === 0) {
                        for(var i = 0; i < _this.versionData.length; i++) {
                            if(_this.versionData[i].id == id) {
                                _this.versionData.splice(i,1);
                                break;
                            }
                        }
                        _this.id = null;
                        _this.dialogDeleteVisible = false;
                    } else {
                        _this.$message.error(_this.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程中发生错误");
            })
        },
        beforeUpload: function (file,fileList) {
            var isAPK = file.type === "application/vnd.android.package-archive";
            var isLt10M = file.size / 1024 / 1024 < 10;
            if (!isAPK) {
                this.$message.error('上传文件格式错误!!');
            }
            if (!isLt10M) {
                this.$message.error('上传apk大小不能超过 10MB!');
            }
            return isAPK && isLt10M;
        },
        handleRemove:function (file,fileList) {
            for(var i = 0; i < fileList.length; i++) {
                if(fileList[i].name == file.name) {
                    fileList.splice(i,1);
                }
            }
        },
        beforeRemove:function (file,fileList) {
            return this.$confirm(`确定移除 ${ file.name }？`);
        },
        handleExceed:function (files,fileList) {
            this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        handleSuccess:function (res, file) {
            var _this = this;
            _this.filePath = res.data.fileAddress;
        },
        toChange:function (id,status) {
            var _this = this;
            _this.id = id;
            _this.status = status;
            _this.dialogChangeVisible = true;
        },
        confirmChange:function () {
            var _this = this;
            var id = _this.id;
            var status = _this.status + 1;
            if(status > 2) {
                _this.$message.warning("状态不符合要求");
                return;
            }
            if(id == null) {
                _this.$message.warning("id不能为空");
            }
            var params = new URLSearchParams();
            params.append("id",id);
            params.append("status",status);
            axios.post(request_prefix + "/cash/version/updateStatus",params)
                .then(function (res) {
                    if(res.data.code === 0) {
                        for (var i = 0; i < _this.versionData.length;i++) {
                            if(_this.versionData[i].id == id) {
                                _this.versionData[i].status = status;
                                break;
                            }
                        }
                        _this.id = null;
                        _this.status = 0;
                        _this.dialogChangeVisible = false;
                    } else {
                        _this.$message.error(res.data.message);
                    }
                })
                .catch(function (res) {
                    _this.$message.error("请求过程发生错误");
                })
        }
    }
});